/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { isNumber } from 'lodash';
import { agent_area_name } from '../../index.d';

function getPriceRender(price: number) {
  return price / 100;
}
function getPriceStorage(price: number) {
  return price * 100;
}
function getRatioRender(price: number) {
  return price / 10;
}
function getRatioStorage(price: number) {
  return price * 10;
}
function getPlatformRatio(providerRatio: string) {
  if (!isNumber(+providerRatio)) return [];
  const platformRatio = 100 - +providerRatio;
  return [platformRatio / 10, +providerRatio / 10];
}

function arrPop(data: any, len: number) {
  return data.map((item: any) => item.slice(0, len));
}

function getAddressList(data: agent_area_name[]) {
  const allList: string[] = [];
  data.forEach((item1) => {
    const keys = ['province', 'city', 'district', 'town'];
    keys.forEach((item2) => {
      if (typeof item1[item2] !== 'string') {
        const addressKeys = Array.from({ length: keys.indexOf(item2) }, (v, k) => k);
        const prefix = addressKeys.map((item3) => item1[keys[item3]]).join('');
        if (item1[item2]) {
          allList.push(...item1[item2].map((item4: string) => prefix + item4));
        }
      }
    });
  });
  return allList;
}

export {
  getPriceRender,
  getPriceStorage,
  getRatioRender,
  getRatioStorage,
  getPlatformRatio,
  arrPop,
  getAddressList,
};
